package com.hejian.android.a72ggametools.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.hejian.android.a72ggametools.R;
import com.hejian.android.a72ggametools.bean.MoneyGame;
import com.hejian.android.a72ggametools.imageloader.DoubleCache;
import com.hejian.android.a72ggametools.imageloader.ImageCache;
import com.hejian.android.a72ggametools.imageloader.ImageLoader;
import com.hejian.android.a72ggametools.viewholder.MoneyGameViewHolder;

import java.util.List;

/**
 * Created by Administrator on 2016/1/15.
 */
public class GameSearchAdapter extends BaseAdapter {
    List<MoneyGame> moneyGameList;
    private Context context;
    private MoneyGameViewHolder moneyGameViewHolder;
    private ImageLoader imageLoader;
    private ImageCache imageCache = new DoubleCache();

    public GameSearchAdapter(List<MoneyGame> moneyGameList, Context context) {
        this.moneyGameList = moneyGameList;
        this.context = context;
        imageLoader = new ImageLoader(context);
        imageLoader.setImageCache(imageCache);
    }

    @Override
    public int getCount() {
        return moneyGameList.size();
    }

    @Override
    public Object getItem(int position) {
        return moneyGameList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null){
            view = LayoutInflater.from(context).inflate(R.layout.money_listview_item,null);
            moneyGameViewHolder = new MoneyGameViewHolder();
            moneyGameViewHolder.imageView = (ImageView) view.findViewById(R.id.money_item_img);
            moneyGameViewHolder.titleTextView = (TextView) view.findViewById(R.id.money_item_title);
            moneyGameViewHolder.ratingBar = (RatingBar) view.findViewById(R.id.money_rating_bar);
            moneyGameViewHolder.button = (Button) view.findViewById(R.id.money_item_button);
            moneyGameViewHolder.description = (TextView) view.findViewById(R.id.money_item_description);
            view.setTag(moneyGameViewHolder);
        }else {
            moneyGameViewHolder = (MoneyGameViewHolder) view.getTag();
        }
        if (moneyGameList != null && moneyGameList.size() >0){
            MoneyGame moneyGame = moneyGameList.get(position);
            moneyGameViewHolder.titleTextView.setText(moneyGame.getName());
            moneyGameViewHolder.ratingBar.setMax(5);
            moneyGameViewHolder.ratingBar.setRating(Float.valueOf(moneyGame.getScore())/2);
            moneyGameViewHolder.description.setText(moneyGame.getCountDL()+"人下载  "+moneyGame.getSize());
            moneyGameViewHolder.button.setVisibility(View.INVISIBLE);
            moneyGameViewHolder.imageView.setImageBitmap(null);
            imageLoader.displayImage(moneyGame.getIcon(),moneyGameViewHolder.imageView);

        }
        return view;
    }

    public void freshView(List<MoneyGame> moneyGameList){
        this.moneyGameList = moneyGameList;
        notifyDataSetChanged();

    }
}
